<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/const.php' );

$forms = $_GET;
$db = new Class_DB;

$sql = "SELECT id FROM D_SAMPLE";


$arr_result[''] = '';
/****  キーワード（製品名・納品先・依頼者名 ****/
$where1 = "";
if( $forms['keyword'] != '' ){
	$where1 = "(company LIKE '%" . $db->quote( $forms['keyword'] ) . "%' OR request_name LIKE '%" . $db->quote( $forms['keyword'] ) . "%' OR search_text LIKE '%" . $db->quote( $forms['keyword'] ) . "%') AND ";
}

/**** 作業状況 ****/
$where2 = "";
if( $forms['status'] ){
	$where2 = sprintf("status = '%s' AND ", $forms['status'] );
}

/****  フォロー記入 ****/
$where3 = ""; $where4 = "";
if( isset( $forms['follow'] ) ){
	if( !(in_array( 1, $forms['follow'] ) && in_array( 2, $forms['follow'] ) ) ){
		if( in_array( 1, $forms['follow'] ) ){
			$where3 = "(follow_text1 <> '' OR follow_text2 <> '') AND ";
		}
		if( in_array( 2, $forms['follow'] ) ){
			$where4 = "(follow_text1 = '' AND follow_text2 = '') AND ";
		}
	}
}

/**** 登録日 ****/
$where5 = ""; $where6 = "";
if( $forms['date1'] != '' ){
	$where5 = sprintf("r_date >= '%s 00:00:00' AND ", $forms['date1'] );
}
if( $forms['date2'] != '' ){
	$where6 = sprintf("r_date <= '%s 23:59:59' AND ", $forms['date2'] );
}

if( $where1 . $where2 . $where3 . $where4 . $where5 . $where6 != "" ){
	$sql .= " WHERE ";
	$sql .= $where1 . $where2 . $where3 . $where4 . $where5 . $where6;

	$sql = substr( $sql, 0, strlen($sql) -5 );
}

$db->query( $sql );
$id = array();
while( $row = $db->fetch() ){
	$id[] = $row['id'];
}

if( !count( $id ) ) exit;

$head = "依頼No., 登録日時, 依頼者名,種類,名称,型番,サイズ,数量,単位,材料費,材料費合計,目的/備考,郵便番号,住所,電話番号,会社名,部署,氏名,特記事項,作業状況,確定納期,フォロー,フォロー1日付,フォロー1内容,フォロー2日付,フォロー2内容";

$str_id = implode( $id, ",");

$sql = sprintf( "SELECT * FROM D_SAMPLE_DETAIL A, D_SAMPLE B WHERE A.id IN ( %s ) AND A.id = B.id", $str_id );
$db->query( $sql );

$data = array();

while( $row = $db->fetch() ){
	
	$row['id'] = 'A' . sprintf('%05d', $row['id']);
	
	$data[] = mb_convert_encoding( sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"",
			$row['id'],
			$row['r_date'],
			$row['request_name'],
			$category[$row['category']],
			$row['pro_name'],
			$row['model'],
			$row['size'],
			$row['amount'],
			$row['unit'],
			$row['cost'],
			$row['cost'] * $row['amount'],
			$row['biko'],
			$row['zipcode'],
			$row['address'],
			$row['tel'],
			$row['company'],
			$row['division'],
			$row['cust_name'],
			$row['memo'],
			$arr_status[$row['status']],
			$row['settle_date'],
			$arr_result[$row['follow_result']],
			$row['follow_date1'],
			$row['follow_text1'],
			$row['follow_date2'],
			$row['follow_text2']
		),
		"sjis",
		"utf8"
	);
}

$filename = date("YmdHis") . '.csv';

$head = mb_convert_encoding( $head, "sjis", "utf8");


header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename=' . $filename );

print $head;
print "\n";
print implode( $data, "\n" );
?>